//
//  ImageGridPropertyList.m
//  ImageGridCell
//
//  Created by nhn on 14-7-26.
//  Copyright (c) 2014年 为为. All rights reserved.
//

#import "ImageGridPropertyList.h"

@interface ImageGridPropertyList ()
{
    NSMutableArray* entryArray;
	NSString* originFilePath;
}

@end

@implementation ImageGridPropertyList

- (id)init
{
    return [self initWithPropertyListFile:nil];
}

- (id)initWithPropertyListFile:(NSString *)filePath
{
    if (self = [super init]) {
		originFilePath = filePath;
        if (filePath && [[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
            entryArray = [NSMutableArray arrayWithContentsOfFile:filePath];
        } else {
            entryArray = [NSMutableArray array];
        }
    }
    return self;
}

- (void)saveToFile
{
	if (originFilePath) {
		[entryArray writeToFile:originFilePath atomically:YES];
	}
}

- (int)numberOfEntrys
{
	return (int)entryArray.count;
}

- (ImageGridPropertyEntry *)entryAtIndex:(int)index
{
	NSMutableDictionary* dict = [entryArray objectAtIndex:index];
	ImageGridPropertyEntry* entry = [[ImageGridPropertyEntry alloc] initWithDict:dict];
	return entry;
}

- (void)removeEntryAtIndex:(int)index
{
	[entryArray removeObjectAtIndex:index];
}

- (void)removeAllEntry
{
	[entryArray removeAllObjects];
}

- (void)addEntry:(ImageGridPropertyEntry *)entry
{
	[entryArray addObject:entry.dictionary];
}

@end
